package integration.json;

import java.util.ArrayList;

import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

public class ConvertJSON {
	
	
	
	public List<DatosRanking> convertJson(String json ){
		
		
		JSONObject jsnobject;
		List<DatosRanking> rankings= new ArrayList<DatosRanking>();
		
		try {
			jsnobject = new JSONObject(json);
			 
			 JSONArray jsonArray = jsnobject.getJSONArray("rankings");
			    for (int i = 0; i < jsonArray.length(); i++) {
			        JSONObject explrObject = jsonArray.getJSONObject(i);
			        String objectString= explrObject.toString();
					int pos1=objectString.indexOf(",");
					int auxpos=objectString.indexOf("p");
					int pos2=objectString.indexOf(":",auxpos)+1;
					int pos3=objectString.indexOf("}");
					int cod= Integer.parseInt(objectString.substring(18, pos1));
					String s= objectString.substring(pos2,pos3);
					int posi = Integer.parseInt(s);
			       DatosRanking r= new DatosRanking(cod, posi);
			       rankings.add(r);
			    }
			        
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		    return rankings;
		    

	}

}
